package com.jnmd.controller;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jnmd.pojo.Course;
import com.jnmd.pojo.QueryTsc;
import com.jnmd.pojo.Speaker;
import com.jnmd.pojo.Video;
import com.jnmd.service.CourseService;
import com.jnmd.service.SpeakerService;
import com.jnmd.service.VideoService;
import com.jnmd.utils.FormarTime;
import com.jnmd.utils.Page;

@Controller
public class VideoController {
	
	@Autowired
	private VideoService videoService;

	@Autowired
	private SpeakerService speakerService;
	
	@Autowired
	private CourseService courseService;
	
	
	@RequestMapping("/videoList.action")
	public String showVideoList(QueryTsc queryTsc,Model model){
		
		queryTsc.setQueryCount(5);
		queryTsc.setStart((queryTsc.getPage()-1)*queryTsc.getQueryCount());
		
		List<Video> allVideo = videoService.getAllVideo(queryTsc);
		for (Video video : allVideo) {
			video.setShowTime(FormarTime.time(video.getTime()));
		}
		
		int count=videoService.getCountByQueryTsc(queryTsc);
		
//		查询组件   需要一个Page对象
		Page<Video> page =new Page<Video>();
		page.setPage(queryTsc.getPage());//当前页
		page.setRows(allVideo);//需要展示的数据
		page.setSize(queryTsc.getQueryCount());
		page.setTotal(count);
		
		
		model.addAttribute("page", page);
		
		List<Speaker> speakerList = speakerService.getAllSpeaker();
		model.addAttribute("speakerList", speakerList);
		
		List<Course> course = courseService.getAllCourse();
		model.addAttribute("courseList", course);
		
		
		return "admin/video/videoManage";
	}
	
	@RequestMapping("/addVideo.action")
	public String editVideo(Model model){
		
		List<Speaker> speakerList = speakerService.getAllSpeaker();
		model.addAttribute("speakerList", speakerList);
		
		List<Course> course = courseService.getAllCourse();
		model.addAttribute("courseList", course);
		
		return "admin/video/addVideo";
	}
	
	@RequestMapping("/saveOrUpdateVideo.action")
	public String updateVideo(Video video,Model model){
		if(null==video.getId() || video.getId()==0){
			
			video.setPlayNum(0);
			videoService.saveVideo(video);
		}else{
			
			videoService.updateVideo(video);
		}
		
		return "redirect:videoList.action";
		
	}

	@RequestMapping("/editVideo.action")
	public String editVideo(int id,Model model){
		 Video video = videoService.findVideoById(id);
		 model.addAttribute("video", video);
		    
			List<Speaker> speakerList = speakerService.getAllSpeaker();
			model.addAttribute("speakerList", speakerList);
			
			List<Course> course = courseService.getAllCourse();
			model.addAttribute("courseList", course);
		 
		 return "admin/video/addVideo";
	}
	
	@RequestMapping("/deleteVideo.action")
	@ResponseBody //处理结果 不走视图解析器，直接将数据转换为json对象来处理
	public String deleteVideo(int id){
		int deleteCount = videoService.deleteVideoById(id);
		System.out.println(deleteCount);
		 return "success";
	}
	
	@RequestMapping("batchDelete.action")
	public String batchDelete(Integer[] ids){
		
		int deleteCount = videoService.batchDelete(ids);
		System.out.println(deleteCount);
		return "redirect:videoList.action";
	}
	
	
	
}
